﻿/*
 * Created by SharpDevelop.
 * User: Matjaž
 * Date: 1.1.2009
 * Time: 11:20
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
using System;
using System.IO;
using System.Windows.Forms;

namespace gopher
{
	/// <summary>
	/// Description of LanguagePreferences.
	/// </summary>
	public partial class LanguagePreferences : Form
	{
		string settingsDir = "";
		public LanguagePreferences(string dir)
		{
			//
			// The InitializeComponent() call is required for Windows Forms designer support.
			//
			InitializeComponent();
			
			settingsDir = dir;
		}
		
		void LanguagePreferencesLoad(object sender, EventArgs e)
		{
			try
			{
				StreamReader SR = new StreamReader(settingsDir+"languages.dat");
				string line;
				while(SR.EndOfStream == false)
				{
					line = SR.ReadLine();
					langBox.Items.Add(line.Substring(line.IndexOf(";")+1).Trim());
					langCodeBox.Items.Add(line.Substring(0,line.IndexOf(";")).Trim());
				}
				SR.Close();
			}
			catch(Exception ex)
			{
				MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
			}
		}
		
		void UpButtonClick(object sender, EventArgs e)
		{
			// Up
			int i = this.langBox.SelectedIndex;
			object o = this.langBox.SelectedItem;
			object o2 = this.langCodeBox.Items[i];

			if (i > 0)
			{
				this.langBox.Items.RemoveAt(i);
				this.langCodeBox.Items.RemoveAt(i);
				this.langBox.Items.Insert(i - 1, o);
				this.langCodeBox.Items.Insert(i - 1, o2);
				this.langBox.SelectedIndex = i - 1;
				this.langCodeBox.SelectedIndex = i - 1;
			}
		}
		
		void LangBoxSelectedIndexChanged(object sender, EventArgs e)
		{
			int index = this.langBox.SelectedIndex;
			this.langCodeBox.SelectedIndex = this.langBox.SelectedIndex;
			//language.Text = langBox.Items[index].ToString();
			//code.Text = langCodeBox.Items[index].ToString();
		}
		
		void DownButtonClick(object sender, EventArgs e)
		{
			// Down
            int i = this.langBox.SelectedIndex;
			object o = this.langBox.SelectedItem;
			object o2 = this.langCodeBox.Items[i];

            if (i < this.langBox.Items.Count - 1)
            {
                this.langBox.Items.RemoveAt(i);
				this.langCodeBox.Items.RemoveAt(i);
				this.langBox.Items.Insert(i + 1, o);
				this.langCodeBox.Items.Insert(i + 1, o2);
				this.langBox.SelectedIndex = i + 1;
				this.langCodeBox.SelectedIndex = i + 1;
            }
		}
		
		void SaveButtonClick(object sender, EventArgs e)
		{
			try
			{
				StreamWriter SW = new StreamWriter(settingsDir+"languages.dat");
				for(int i = 0; i < langBox.Items.Count; i++)
				{
					SW.WriteLine(langCodeBox.Items[i] + "; " + langBox.Items[i]);
				}
				SW.Flush();
				SW.Close();
			}
			catch(Exception ex)
			{
				MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
			}
		}
		
		void AddButtonClick(object sender, EventArgs e)
		{
			if((language.Text != string.Empty) && (code.Text.Contains("_")))
			{
				langBox.Items.Add(language.Text.Trim());
				langCodeBox.Items.Add(code.Text.Trim());
			}
		}
		
		void LangCodeBoxSelectedIndexChanged(object sender, EventArgs e)
		{
			int index = this.langBox.SelectedIndex;
			this.langBox.SelectedIndex = this.langCodeBox.SelectedIndex;
			//language.Text = langBox.Items[index].ToString();
			//code.Text = langCodeBox.Items[index].ToString();
		}
		
		void RemoveButtonClick(object sender, EventArgs e)
		{
			int index = this.langBox.SelectedIndex;
			this.langBox.Items.RemoveAt(index);
			this.langCodeBox.Items.RemoveAt(index);
		}
		
		void LanguagePreferencesHelpRequested(object sender, HelpEventArgs hlpevent)
		{
			Help.ShowHelp(this, Application.ExecutablePath.Substring(0, Application.ExecutablePath.LastIndexOf("\\"))+"\\Help\\gopher.chm", "html\\pluslang-dialog.htm");
		}
	}
}
